package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDictSon;
import com.ruoyi.system.service.ISysDictSonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dict/son")
public class SysDictSonController extends BaseController {

    @Resource
    ISysDictSonService iSysDictSonService;

    private String prefix = "system/dict/son";

    @GetMapping("/toDetailSon/{dictCode}")
    public String toDetailSon(@PathVariable("dictCode") String dictCode, ModelMap mmap) {
        mmap.put("dictCode", dictCode);
        return prefix + "/son";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        startPage();
        List<Map<String, Object>> list = iSysDictSonService.findDictSonListByDictCode(map.get("dictCode").toString());
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(String dictCode, ModelMap mmap) {
        mmap.put("dictCode", dictCode);
        return prefix + "/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(iSysDictSonService.add(map));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Map map = iSysDictSonService.findDictSonById(id);
        mmap.put("son", map);
        return prefix + "/edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(iSysDictSonService.edit(map));
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(iSysDictSonService.deleteSonByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 首页面 -> 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SysDictSon> util = new ExcelUtil<SysDictSon>(SysDictSon.class);
        return util.importTemplateExcel("字典模板");
    }

    @RequestMapping("/importData")
    @ResponseBody
    public AjaxResult importDataMater(MultipartFile file, String dictCode) throws Exception {
        ExcelUtil<SysDictSon> util = new ExcelUtil<SysDictSon>(SysDictSon.class);
        List<SysDictSon> list = util.importExcel(file.getInputStream());
        String message = iSysDictSonService.importSonData(list, dictCode);
        return AjaxResult.success(message);
    }
}
